//
//  UIImageView+PYWebImage.m
//  IHome
//
//  Created by Jim on 2019/4/6.
//  Copyright © 2019 com.zhujizheng. All rights reserved.
//

#import "UIImageView+PYWebImage.h"
#import <objc/runtime.h>

@interface UIImageView ()
// 记录请求相关的信息
//@property NSMutableDictionary *py_imgInfoDict;

@end

@implementation UIImageView (PYWebImage)
#pragma mark - py_imgInfoDict
//- (NSMutableDictionary *)py_imgInfoDict{
//    return objc_getAssociatedObject(self, _cmd);
//}
//
//- (void)setPy_imgInfoDict:(NSMutableDictionary *)py_imgInfoDict{
//    objc_setAssociatedObject(self, @selector(py_imgInfoDict), py_imgInfoDict, OBJC_ASSOCIATION_RETAIN);
//}

- (void)py_setImageURLString:(NSString *)imgUrl
                 placeholder:(UIImage *)placeholder
                     options:(YYWebImageOptions)options
                    progress:(nullable void (^)(double))progressHandler
                   transform:(YYWebImageTransformBlock)transform
                  completion:(void (^)(UIImage * _Nonnull))completion
{
    if ([imgUrl containsString:@"pq1iybgdl.bkt.clouddn.com"]) {
        imgUrl = [imgUrl stringByReplacingOccurrencesOfString:@"pq1iybgdl.bkt.clouddn.com" withString:@"media.zhujizheng.com"];
    }
    if ([imgUrl containsString:@"file.zhujizheng.com"]) {
        imgUrl = [imgUrl stringByReplacingOccurrencesOfString:@"file.zhujizheng.com" withString:@"media.zhujizheng.com"];
    }
    if (imgUrl.length == 0) {
        NSLog(@"图片url为空, imgUrl = %@", imgUrl);
        return;
    }
//    NSLog(@"imgUrl = %@", imgUrl);
    [self py_setOrRetryImageURLString:imgUrl
                          placeholder:placeholder
                              options:options
                             progress:progressHandler
                            transform:transform
                           completion:completion];
}

- (void)py_setOrRetryImageURLString:(NSString *)imgUrl
                        placeholder:(UIImage *)placeholder
                            options:(YYWebImageOptions)options
                           progress:(nullable void (^)(double))progressHandler
                          transform:(YYWebImageTransformBlock)transform
                         completion:(nullable void (^)(UIImage * _Nonnull image))completion
{
    [self setImageWithURL:[NSURL URLWithString:imgUrl] placeholder:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        if (expectedSize <= 0) {
            return ;
        }
        double progressValue = (double)receivedSize / (double)expectedSize;
        NSLog(@"progressValue = %f", progressValue);
        if (progressHandler) {
            progressHandler(progressValue);
        }
    } transform:transform completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (error) {
            NSLog(@"imgUrl = %@, error = %@", imgUrl, error);
            return;
        }
        
        if (completion && image) {
            completion(image);
        }
    }];
}

@end
